package com.julyup.core.controller;

import com.julyup.core.entity.Company;
import com.julyup.core.service.CompanyService;
import com.julyup.core.service.HCSearchService;
import com.julyup.core.util.ExportUtils;
import com.julyup.core.util.ResponseObject;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 *
 * @Author: zhenzhong.wang@honeywell.com
 * @Date: 2019/4/18 4:29 PM
 */
@RestController
public class CompanyController {

  @Autowired
  HCSearchService hcSearchService;

  @Autowired
  CompanyService companyService;

  @PostMapping("/api/company/hc")
  public ResponseObject hcCompany(){
    hcSearchService.start();
    return ResponseObject.success(null);
  }


  @PostMapping("/api/company/aliyun")
  public ResponseObject aliyunCompany(){
    hcSearchService.start();
    return ResponseObject.success(null);
  }

  @GetMapping("/api/company/excel/file")
  public ResponseObject exportExcelToFile() throws IOException {
    List<Company> companies = companyService.findAll();
    ExportUtils.exportExcelToFile(companies, "/Users/zhenzhong/Desktop/慧聪企业信息.xlsx");
    return ResponseObject.success("OK");
  }

  @GetMapping("/api/company/excel/web")
  public ResponseObject exportExcelToResponse(HttpServletResponse response) throws IOException {
    List<Company> companies = companyService.findAll();
    ExportUtils.exportExcelToResponse(companies, "慧聪企业信息.xlsx", response);
    return ResponseObject.success("OK");
  }
}
